<template>
  <div>
    <el-form ref="formRef" :model="form" v-bind="config">
      <template v-for="(field, index) in config.fields">
        <field v-model="form[field.prop]" :config="field" />
      </template>
      <el-form-item>
        <el-button type="primary" @click="submitForm(formRef)">
          Create
        </el-button>
        <el-button @click="resetForm(formRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import field from './components/field.vue'
const form = ref({})
const formRef = ref()
const config = {
  labelWidth: '100px',
  fields: [
    { prop: 'name', label: '姓名', type: 'el-input' },
    { prop: 'age', label: '年龄', type: 'el-input' },
    {
      prop: 'sex',
      label: '性别',
      type: 'el-select',
      options: [
        { label: '男', value: '男' },
        { label: '女', value: '女' }
      ]
    },
  ]
}
const submitForm = async (formEl) => {
  try {
    await formEl.validate()
  } catch (error) {
    console.log('表单验证失败', form.value)
  }
}

const resetForm = (formEl) => {
  formEl.resetFields()
}
</script>